package com.cangxuan.app.ui.user;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.alibaba.fastjson.JSONObject;
import com.cangxuan.app.R;
import com.cangxuan.app.ui.BaseActivity;
import com.cangxuan.app.utils.DownloadUtils;
import com.cangxuan.app.utils.HttpUtils;

import java.io.File;
import java.net.HttpURLConnection;
import java.util.Map;

/**
 * Author: 杨刚，彭伟辰
 * Activity: 关于藏选
 */
public class MyAboutActivity extends BaseActivity {

    Handler handler=new Handler();
    Button updateButton;
    TextView aboutTextVersion;
    AlertDialog.Builder alertDialogBuilder;
    String version;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_about);
        setTitle("关于藏选");
        updateButton=findViewById(R.id.about_update);
        aboutTextVersion=findViewById(R.id.about_text_version);
        version=getResources().getString(R.string.app_version);
        aboutTextVersion.setText("v"+version);
        alertDialogBuilder = new AlertDialog.Builder(this);
        updateButton.setOnClickListener(view -> {
            setUpdateOnClick("/api/version/ver");
        });
    }
    public void setUpdateOnClick(String url){
        ProgressDialog progressDialog = ProgressDialog.show(this, "", "请稍等...");
        Thread thread=new Thread(() -> {
            String s = HttpUtils.get(url);
            JSONObject jsonRs=JSONObject.parseObject(s);
            if (jsonRs == null) {
                handler.post(()->{
                    Toast.makeText(context, "服务器异常", Toast.LENGTH_SHORT).show();
                });
                return;
            }
            if(jsonRs.getInteger("status")== HttpURLConnection.HTTP_OK){
                Map value = jsonRs.getObject("value",Map.class);
                String illustrate=value.get("illustrate").toString();
                String versionNumber=value.get("versionNumber").toString();
                Boolean isItMandatory=new Boolean(value.get("isItMandatory").toString());
                String ur=HttpUtils.host+ File.separator+"cangxuan."+versionNumber+".apk";

                handler.post(()->{
                    if((!versionNumber.equals(version))&&isItMandatory==true){
                        alertDialogBuilder.setTitle(versionNumber+"版本更新");
                        alertDialogBuilder.setMessage(illustrate);
                        alertDialogBuilder.setPositiveButton("确认更新", (dialogInterface, i) -> {
                            //必须更新的点击事件
                            new DownloadUtils(MyAboutActivity.this, MyAboutActivity.this, ur, "藏选");
                            Toast.makeText(MyAboutActivity.this, "正在下载，请在通知栏中查看下载状态", Toast.LENGTH_LONG).show();
                            updateButton.setText("正 在 下 载");
                            updateButton.setEnabled(false);
                        });
                        alertDialogBuilder.setCancelable(false);
                        progressDialog.dismiss();
                        alertDialogBuilder.show();
                    }else if((!versionNumber.equals(getResources().getString(R.string.app_version)))&&isItMandatory==false){
                        alertDialogBuilder.setTitle(versionNumber+"版本更新");
                        alertDialogBuilder.setMessage(illustrate);
                        alertDialogBuilder.setPositiveButton("确认更新", (dialogInterface, i) -> {
                            //选择更新的点击事件
                            new DownloadUtils(MyAboutActivity.this, this, ur, "藏选");
                            Toast.makeText(MyAboutActivity.this, "正在下载，请在通知栏中查看下载状态", Toast.LENGTH_LONG).show();
                            updateButton.setText("正 在 下 载");
                            updateButton.setEnabled(false);
                        });
                        alertDialogBuilder.setNegativeButton("取消更新",(dialogInterface, i)->{
                            //选择取消更新的点击事件
                        });
                        alertDialogBuilder.setCancelable(true);
                        progressDialog.dismiss();
                        alertDialogBuilder.show();
                    }else {
                        progressDialog.dismiss();
                        Toast.makeText(getApplicationContext(),"当前已是最新版本", Toast.LENGTH_SHORT).show();
                    }
                });
            }else {
                handler.post(()->{
                    progressDialog.dismiss();
                    Toast.makeText(getApplicationContext(), jsonRs.getString("message"), Toast.LENGTH_SHORT).show();
                    finish();
                });
            }
        });
        thread.start();
    }
}
